//
//  AlertController.m
//  VinSwiper
//
//  Created by siloon on 5/19/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SLAlertController.h"

@implementation SLAlertController

@synthesize callback = _callback;
@synthesize alertView = _alertView;

+ (SLAlertController *)controllerWithCallback:(SLCallback *)callback{
	SLAlertController *instance = [[SLAlertController alloc] init];
	instance.callback = callback;
	return instance;
}

- (id)init {
    self = [super init];
    if (self) {
        InitAndDeallocLog(@"AlertController init");
    }
    return self;
}

- (void)dealloc {
	InitAndDeallocLog(@"AlertController dealloc");
    self.callback = nil;
	self.alertView = nil;
    [super dealloc];
}

#pragma mark -

- (void)showWithTitle:(NSString *)title message:(NSString *)msg buttons:(NSArray *)btns{
	UIAlertView *altv = [[UIAlertView alloc] initWithTitle:title 
													message:msg delegate:self 
										  cancelButtonTitle:[btns objectAtIndex:0] 
										  otherButtonTitles:nil];
	BOOL isFirst = YES;
	for(NSString *btn  in btns){
		if(isFirst){
			isFirst = NO;
		}else{
			[altv addButtonWithTitle:btn];
		}
	}
	
	self.alertView = altv;
	
	[altv show];
	
	[altv release];
	
}

#pragma mark - delegate

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
	NSNumber *number = [NSNumber numberWithInt:buttonIndex];
	[self.callback.target performSelector:self.callback.selector withObject:number];
	[self release];
}

@end
